package com.training1.api.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.training1.core.common.global.Global;
import com.training1.core.common.utils.security.MD5;
import com.training1.core.dto.APIRegisterDto;
import com.training1.core.dto.APIResultDto;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.service.MemberService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;


@Api(value = "/api/register", description = "会员注册") // Swagger annotation
@RestController
@RequestMapping("/api/register")
public class APIRegisterController {
	
	private static final Logger logger = LoggerFactory.getLogger(APIRegisterController.class);
	
	@Resource
	private MemberService memberService;
	
	@ApiOperation(value = "api会员注册, 第一个参数是会员手机号码，第二个参数是会员密码,第三个参数是重复密码,第四个参数是验证码")
	@RequestMapping(value = "/appregister", method = RequestMethod.GET)
	public APIResultDto<Object> register(@RequestParam String cellphone,@RequestParam String password,
			@RequestParam String pepassword,@RequestParam String identifying) {
		logger.info("客户端会员注册, cellphone : " + cellphone);
		APIResultDto<Object> dto = new APIResultDto<Object>();
		/**
		 * 验证密码和重复密码是否一致
		 * 0.注册成功 
		 * 2.该手机号用户已存在，无法再次注册 
		 * 3.两次输入密码不一致，请重新输入 
		 * 1.您输入的手机号格式不正确 
		 * 5.密码必须输入6-16位字符							
		 */
		String regex ="^[1]\\d{10}$";
		if(cellphone==null || !cellphone.matches(regex)){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("1");
			//您输入的手机号格式不正确 
			dto.setMessage(Global.getMessage("api.error.register.telsize"));
			return dto;
			
		}else if(identifying==null || identifying.length()!=6){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("2");
			//验证码长度应为6位
			dto.setMessage(Global.getMessage("api.error.register.identifyingsize"));
			return dto;
			
		}else if(password.length()>16 || password.length()<6){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("3");
			//密码必须输入6-16位字符
			dto.setMessage(Global.getMessage("api.error.register.passwordsize"));
			return dto;
			
		}else if(!password.equals(pepassword)){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("4");
			//两次输入密码不一致，请重新输入
			dto.setMessage(Global.getMessage("api.error.register.passworderr"));
			return dto;
		}
		//根据手机号查找会员是否被注册,,该手机号用户已存在，无法再次注册
		MemberEntity member = memberService.findMember(cellphone);
		if(member != null){
			dto.setStatus(APIResultDto.ERROR);
			dto.setErrorCode("5");
			//两次输入密码不一致，请重新输入
			dto.setMessage(Global.getMessage("api.error.register.telexist"));
			return dto;
		}
		//注册成功
		//存注册信息
		APIRegisterDto registerDto = new APIRegisterDto();
		registerDto.setCellphone(cellphone);
		registerDto.setPassword(MD5.getMD5(password));
		registerDto.setIdentifying(identifying);
		memberService.register(registerDto);

		dto.setStatus(APIResultDto.SUCCESS_OK);
		dto.setErrorCode("0");
		
		//注册成功
		dto.setMessage(Global.getMessage("api.error.register.success"));
		return dto;
	}

}
